package com.zdw.test3;

import com.zdw.test2.utils.JdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * Author: Zhang Dongwei
 * Date: 2022/4/3 20:25
 */
public class TestSelect {
    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement st = null;
        ResultSet rs = null;
        try {
            conn = JdbcUtils.getConnection();
            // 区别
            //使用问号占位符代替参数
            String sql = "select * from users where id=?";
            st = conn.prepareStatement(sql); // 预编译SQL，现写SQL，然后不执行
            // 手动给参数赋值
            st.setInt(1,1);

            // 执行
            rs = st.executeQuery();
            if(rs.next()){
                System.out.println(rs.getString("NAME"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JdbcUtils.release(conn,st,rs);// 因为没有用到resultset结果集，所以直接传入null即可
        }
    }
}
